// litgroups_txn.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing literal groups
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h"


//////////////////////////////////////////////////////////////////////////////
//
// LIT_GROUP Transactions
//
//////////////////////////////////////////////////////////////////////////////



TRANSACTION_CODE( RevLitGroup ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCBuffer* Lit_Group = NULL;
	XCBuffer* Literal_Values = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( RevLitGroup );
		//@ Create transaction defn buffer
		TransBuf = App->CreateBuffer( FMT_LIT_RELATE );

		//@ Create a LIT_GROUP buffer
		Lit_Group = App->CreateBuffer( FMT_LIT_GROUP );

		//@ Create a LITERAL_VALUES buffer
		Literal_Values = App->CreateBuffer( FMT_LITERAL_VALUES );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		Lit_Group->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		Literal_Values->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			//@ SUB LitGroupVerify to verify and retrieve record
			LitGroupVerify( Lit_Group, &txnvars, VERIFY_REVIEW, false );

			//@ Send back header buffer
			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
			txnvars.buffer->MoveCorresponding( Lit_Group, txnvars.Errors );
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			//@ Move corresponding from lit_group to lit_relate record
			TransBuf->MoveCorresponding( Lit_Group, NULL );

			int child = txnvars.buffer->GetChildFormat();
			if (!child)
				child = FMT_ADDLITTOGROUP;

			detail = App->CreateBuffer( child, true );

			// Find out which literals are in the literal group
			stmt = GetLitGroup( TransBuf, &txnvars );
			while (!stmt->Eof()) {
				detail->MoveCorresponding( TransBuf, txnvars.Errors, true );

				// Look up literal name
				if (detail->GetField( FLD_LITERAL_NAME))
					LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_LITERAL_NUMBER, FLD_LITERAL_NAME, FMT_LITERAL_DEFN );

				// Look up group name
				LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_LIT_GROUP_NBR, FLD_LIT_GROUP_NAME, FMT_LIT_GROUP );

				// If the detail buffer has the field LITERAL on it:
				if (detail->GetField( FLD_LITERAL )) {
					//@ GRS

					//@ Move corresponding values from the TransBuf into LITERAL_VALUES
					Literal_Values->MoveCorresponding( TransBuf, txnvars.Errors );

					//@ Retrieve the LITERAL_VALUES record
					LiteralValueRetrieve( Literal_Values, &txnvars );

					//@ Move the field LITERAL back to the detail buffer
					detail->MoveField( Literal_Values, FLD_LITERAL, true );

					//@ GRE
				};

				App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
				TransBuf->Clear();
				stmt->MoveNext();
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 5, &TransBuf, &Lit_Group, &Literal_Values, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};
